<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
	
	case 'addAuthor' :
		addAuthor();
		break;
		
	case 'modifyAuthor' :
		modifyAuthor();
		break;
		
	case 'deleteAuthor' :
		deleteAuthor();
		break;
	
	case 'deleteImage' :
		deleteImage();
		break;
    

	default :
	    // if action is not defined or unknown
		// move to main Author page
		header('Location: index.php');
}


function addAuthor()
{
    $nhomnc_id   = $_POST['cboGroup'];
    $matg        = $_POST['txtMatg'];
	$hotentg	 = $_POST['txtHotentg'];
	$ngaysinh    = $_POST['txtNgaysinh'];
	$quequan     = $_POST['txtQuequan'];
	$madv		 = $_POST['txtMadv'];
	$chucvu		 = $_POST['txtChucvu'];
	$hocham      = $_POST['txtHocham'];
	$hocvi	     = $_POST['txtHocvi'];
	
	$images = uploadAuthorImage('fleImage', SRV_ROOT . 'images/product/');

	$mainImage = $images['image'];
	$thumbnail = $images['thumbnail'];
	
	$sql1   = "INSERT INTO tacgia (matg, hotentg, ngaysinh, quequantg, madv, chucvu, hocham, hocvi)
	          VALUES ('$matg', '$hotentg', '$ngaysinh', '$quequan', '$madv', '$chucvu','$hocham', '$hocvi')";

	$result1 = dbQuery($sql1);
	$sql2 	= "select id from tacgia where matg = '$matg'";
	
	$result2 = dbQuery($sql2) or die('' . mysql_error());
	
	if (mysql_num_rows($result2)<>0){
		while ($row = mysql_fetch_array($result2))
			$tg_id = $row["id"];
	}
		
	$sql3   = "INSERT INTO tacgia_nhomnc(tg_id, nhomnc_id)
	          VALUES($tg_id, $nhomnc_id)";
	
	$result1 = dbQuery($sql3);
	
	header("Location: index.php?nhomnc_id=$nhomnc_id");	
}

/*
	Upload an image and return the uploaded image name 
*/
function uploadAuthorImage($inputName, $uploadDir)
{
	$image     = $_FILES[$inputName];
	$imagePath = '';
	$thumbnailPath = '';
	
	// if a file is given
	if (trim($image['tmp_name']) != '') {
		$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];

		// generate a random new file name to avoid name conflict
		$imagePath = md5(rand() * time()) . ".$ext";
		
		list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 

		// make sure the image width does not exceed the
		// maximum allowed width
		if (LIMIT_AUTHOR_WIDTH && $width > MAX_AUTHOR_IMAGE_WIDTH) {
			$result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_AUTHOR_IMAGE_WIDTH);
			$imagePath = $result;
		} else {
			$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
		}	
		
		if ($result) {
			// create thumbnail
			$thumbnailPath =  md5(rand() * time()) . ".$ext";
			$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
			
			// create thumbnail failed, delete the image
			if (!$result) {
				unlink($uploadDir . $imagePath);
				$imagePath = $thumbnailPath = '';
			} else {
				$thumbnailPath = $result;
			}	
		} else {
			// the author cannot be upload / resized
			$imagePath = $thumbnailPath = '';
		}
		
	}

	
	return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}

/*
	Modify a author
*/
function modifyAuthor()
{
	$matg   	= $_GET['matg'];	
    $hotentg    = $_POST['txtHotentg'];
	$ngaysinh 	= $_POST['txtNgaysinh'];
	$quequantg  = $_POST['txtQuequan'];
	$madv 		= $_POST['txtMadv'];
	$chucvu  	= $_POST['txtChucvu'];
	$hocham 	= $_POST['txtHocham'];
	$hocvi 		= $_POST['txtHocvi'];

  
	
	//$images = uploadAuthorImage('fleImage', SRV_ROOT . 'images/product/');

/*	$mainImage = $images['image'];
	$thumbnail = $images['thumbnail'];

	// if uploading a new image
	// remove old image
	if ($mainImage != '') {
		_deleteImage($matg);
		
		$mainImage = "'$mainImage'";
		$thumbnail = "'$thumbnail'";
	} else {
		// if we're not updating the image
		// make sure the old path remain the same
		// in the database
		$mainImage = 'pd_image';
		$thumbnail = 'pd_thumbnail';
	}
	*/		
	$sql1   = "UPDATE tacgia 
               SET hotentg = '$hotentg', ngaysinh = '$ngaysinh', quequantg = '$quequantg', madv = '$madv', chucvu = '$chucvu', hocham = '$hocham', hocvi = '$hocvi'
			   WHERE matg = '$matg'" ;
         
			   

	$result = dbQuery($sql1);
	
	header('Location: index.php');			  
}

/*
	Remove a author
*/
function deleteAuthor()
{/*

	if (isset($_GET['matg']) && (int)$_GET['matg'] !='') {
		$matg = $_GET['matg'];
	} else {
		header('Location: index.php');
	}
	
	// remove any references to this author from
	// tbl_order_item and tbl_cart
	$sql = "DELETE FROM tbl_order_item
	        WHERE pd_id = $matg";
	dbQuery($sql);
			
	$sql = "DELETE FROM tbl_cart
	        WHERE pd_id = $matg";	
	dbQuery($sql);
			
	// get the image name and thumbnail
	$sql = "SELECT pd_image, pd_thumbnail
	        FROM tbl_author
			WHERE pd_id = $matg";
			
	$result = dbQuery($sql);
	$row    = dbFetchAssoc($result);
	
	// remove the author image and thumbnail
	if ($row['pd_image']) {
		unlink(SRV_ROOT . 'images/product/' . $row['pd_image']);
		unlink(SRV_ROOT . 'images/product/' . $row['pd_thumbnail']);
	}
	
	// remove the author from database;
	$sql = "DELETE FROM tbl_author 
	        WHERE pd_id = $matg";
	dbQuery($sql);
	
	header('Location: index.php?catId=' . $_GET['catId']);*/
}


/*
	Remove a author image
*/
function deleteImage()
{
/*
	if (isset($_GET['matg']) && (int)$_GET['matg'] > 0) {
		$matg = (int)$_GET['matg'];
	} else {
		header('Location: index.php');
	}
	
	$deleted = _deleteImage($matg);

	// update the image and thumbnail name in the database
	$sql = "UPDATE tbl_author
			SET pd_image = '', pd_thumbnail = ''
			WHERE pd_id = $matg";
	dbQuery($sql);		

	header("Location: index.php?view=modify&matg=$matg");
	*/
}

function _deleteImage($matg)
{
/*
	// we will return the status
	// whether the image deleted successfully
	$deleted = false;
	
	$sql = "SELECT pd_image, pd_thumbnail 
	        FROM tbl_author
			WHERE pd_id = $matg";
	$result = dbQuery($sql) or die('Cannot delete author image. ' . mysql_error());
	
	if (dbNumRows($result)) {
		$row = dbFetchAssoc($result);
		extract($row);
		
		if ($pd_image && $pd_thumbnail) {
			// remove the image file
			$deleted = @unlink(SRV_ROOT . "images/product/$pd_image");
			$deleted = @unlink(SRV_ROOT . "images/product/$pd_thumbnail");
		}
	}
	
	return $deleted;*/
}




?>